package com.bjgzy.courselive.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bjgzy.courselive.di.component.DaggerCourseLiveHomeComponent;
import com.bjgzy.courselive.mvp.contract.CourseLiveHomeContract;
import com.bjgzy.courselive.mvp.presenter.CourseLiveHomePresenter;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CourseLiveHomeData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-30 12:41:01
 */
@Route(path = RouterHub.COURSELIVE_COURSELIVEFRAGMENT)
public class CourseLiveHomeFragment extends BaseFragment<CourseLiveHomePresenter> implements CourseLiveHomeContract.View {

    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @Inject
    BaseQuickAdapter<CourseLiveHomeData, AutoBaseViewHolder> mAdapter;
    @Inject
    List<CourseLiveHomeData> lists;
    @Inject
    RecyclerView.LayoutManager mManager;
    private String id;
    private String mProject;

    public static CourseLiveHomeFragment newInstance(String id) {
        CourseLiveHomeFragment fragment = new CourseLiveHomeFragment();
        Bundle args = new Bundle();
        args.putString(Constants.ROUTER_STRING, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerCourseLiveHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
        id = getArguments().getString(Constants.ROUTER_STRING);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.courselive_fragment, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mSwipeLayout.setOnRefreshListener(() -> {
            getDatas(true);
            hideLoading();
        });
        rvList.setLayoutManager(mManager);
        rvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            CourseLiveHomeData data = mAdapter.getData().get(position);
            if (ObjectUtils.isNotEmpty(mProject)) {
                data.setProject(mProject);
            }
            ARouter.getInstance().build(RouterHub.COURSELIVE_COURSEDETAILSACTIVITY)

                    .withString(Constants.ROUTER_IMG, data.getPreImage())
                    .withString(Intent.EXTRA_TITLE, Constants.COURSE_DETAILS)
                    .withInt(Constants.ROUTER_INTEGER, data.getInternalId())
                    .navigation();
        });
        mAdapter.setOnLoadMoreListener(() -> getDatas(false), rvList);
        getDatas(true);
    }

    private void getDatas(boolean pullToRefresh) {
        mPresenter.showDirectList(pullToRefresh, id);
    }

    @Override
    public void setData(@Nullable Object data) {
        this.mProject = (String) data;
    }

    @Override
    public void showLoading() {
        if (ObjectUtils.isNotEmpty(mSwipeLayout)) {
            mSwipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(mSwipeLayout)) {
            mSwipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

}
